<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="操作时间：" prop="createTime">
              <el-date-picker
                v-model="createTime"
                type="daterange"
                class="time220"
                :editable="false"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
                :picker-options="pickerOptions"
                @change="getArticleList"
              />
            </el-form-item>
            <el-form-item label="管理员：" prop="user_id">
              <el-select
                v-model.trim="form.user_id"
                placeholder="管理员"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(user, index) in list.user"
                  :key="index"
                  :label="user"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="模块：" prop="module">
              <el-select
                v-model.trim="form.module"
                placeholder="模块"
                class="wid220"
                @change="getArticleList"
              >
                <el-option label="所有" value=""></el-option>
                <el-option
                  v-for="(operate_module, index) in list.operate_module"
                  :key="index"
                  :label="operate_module"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="moredata">
              <el-button @click="exportConfirm" class="mainor_tea">导出</el-button>
            </MainFormBtn>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-show="moredata">
                <el-form-item label="操作类型：" prop="type">
                  <el-select
                    v-model.trim="form.type"
                    placeholder="操作类型"
                    class="wid220"
                    @change="getArticleList"
                  >
                    <el-option label="所有" value=""></el-option>
                    <el-option
                      v-for="(operate_type, index) in list.operate_type"
                      :key="index"
                      :label="operate_type"
                      :value="parseInt(index)"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="主体：" prop="main">
                  <el-input
                    v-model.trim="form.main"
                    placeholder="主体"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <div class="clearfix"></div>
                <el-form-item label=" ">
                  <MainFormBtn class="btn-bottom" @reset="reset" @getSearchFor="getSearchFor" :loading="listLoading" :showBtn="!moredata">
                    <el-button @click="exportConfirm" class="mainor_tea">导出</el-button>
                  </MainFormBtn>
                </el-form-item>
              </div>
            </el-collapse-transition>
          </el-form>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable"
            :class="{ 'mg-t-6': !moredata }"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            @selection-change="handleSelectionChange"
            :height="borderHeight"
            :header-cell-class-name="this.$verticaline"
            :header-cell-style="tableHeightStyle"
            @header-dragend="changeTableWidth"
          >
            <!-- <el-table-column type="selection"></el-table-column> -->
            <el-table-column
              label="管理员"
              prop="admin.name"
              :width="tableWidth[hash + 'admin.name']"
            />
            <el-table-column
              label="模块"
              prop="module"
              :width="tableWidth[hash + 'module']"
            >
              <template slot-scope="scope">{{
                scope.row.module ? scope.row.module.name : ""
              }}</template>
            </el-table-column>
            <el-table-column
              label="操作"
              prop="operate.name"
              :width="tableWidth[hash + 'operate.name']"
            />
            <el-table-column
              label="主体"
              prop="main"
              :width="tableWidth[hash + 'main']"
            />
            <el-table-column
              label="操作内容"
              prop="operate_content"
              class-name="main_hei"
              :width="tableWidth[hash + 'operate_content']"
            >
              <template slot-scope="scope">
                <!-- v-for="(content, index) in scope.row.content"
                  :key="index" -->
                <div class="marginTop5 marginBottom5">
                  {{ scope.row.content }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="ip"
              prop="ip"
              :width="tableWidth[hash + 'ip']"
            />
            <el-table-column
              label="时间"
              prop="created_at"
              :width="tableWidth[hash + 'created_at']"
            >
              <template slot-scope="scope">{{
                parseTime(scope.row.created_at, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  operate_index,
  export_advice_detail,
  export_global_operation,
} from "@/api/global/globalOperation";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import dateOption from "@/options/dateOption";
import {
  parseTime,
  getWeekStartAndEnd,
  exportMethod,
  getFiveday,
} from "@/utils/index";

export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      // 时间
      exportMethod,
      parseTime,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        start_time: "",
        end_time: "",
        user_id: null,
        module: null,
        type: null,
        main: "",
      },
      createTime: getFiveday(),
      pickerOptions: dateOption,

      // 导出
      selectArr: [],
      exportbuVisible: false,
      exportTime: "",
      mode: "",

      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      moredata: false,
    };
  },
  created() {
    // 列表
    this.getNumerical(210);
    this.operate_index();
  },
  methods: {
    startGetting() {
      this.getSearchFor();
    },
    getSearchFor() {
      this.getArticleList();
    },
    more() {
      this.moredata = !this.moredata;
    },
    // 导出明细
    exportConfirm() {
      let form = this.list_form();

      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      const notify = this.$notify.info({
        title: "开始导出...",
        type: "success",
        duration: 0,
      });
      export_global_operation(data)
        .then((res) => {
          this.exportMethod(res, "操作日志", notify);
        })
        .catch((err) => {
          this.$message.error("导出失败");
        });
      this.exportbuVisible = false;
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.operate_index();
    },
    // 列表
    operate_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current
      );
      operate_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            if (e.content) {
              let content = JSON.parse(e.content);
              let str = "";
              for (let key in content) {
                str += key + "：" + content[key] + ",";
              }
              e.content = str.slice(0, str.length - 1);
            }
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.start_time = this.createTime[0] ? this.createTime[0] : "";
      form.end_time = this.createTime[1] ? this.createTime[1] : "";

      return form;
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item) => {
        this.selectArr.push({ id: item.id });
      });
      console.log(this.selectArr);
    },
    //重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.createTime = getFiveday();
      this.getArticleList();
    },
    // 导出时间范围
    datepicker(val) {
      let start = "";
      let end = "";
      if (val) {
        start = new Date(val[0]).getTime();
        end = new Date(val[1]).getTime();
        if (end - start > 30 * 24 * 60 * 60 * 1000) {
          this.$message.error("选择范围不能超过一个月，请重新选择");
          this.exportTime = "";
        }
      }
    },

    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.operate_index();
    },
    changePage(page) {
      this.current.page = page;
      this.operate_index();
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: 100%;
    .el-form-item {
      margin-bottom: 8px;
    }
  }
}

// 导出订单表头
.exptitle {
  display: block;
  font-size: 16px;
  color: #000000;
  margin: 0 0 10px;
}
.company_name {
  padding-top: 15px;
}
.maintip {
  text-align: center;
  padding-top: 15px;
  font-size: 18px;
  span {
    display: inline;
    padding: 18px 0;
    color: red;
  }
}
.isexportCheckbox {
  margin-top: 15px;
}
.company_name_box {
  .company_name {
    padding-top: 0;
  }
}
</style>
